Skip to content

Commit

Permalink
added lots of new _repr_xyz methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BjornFJohansson committed Dec 4, 2016
1 parent 50ba92c commit 3895997
Show file tree
Hide file tree
Showing 15 changed files with 2,062 additions and 145 deletions.
5 changes: 5 additions & 0 deletions PRIMERS.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

>fw64 t-sequence
atgactgctaacccttc
>rv64 t-sequence
catcgtaagtttcgaac
53 changes: 41 additions & 12 deletions pydna/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2013,2014, 2015 by Björn Johansson. All rights reserved.
# This code is part of the Python-dna distribution and governed by its
# license. Please see the LICENSE.txt file that should have been included
# as part of this package.

'''
pydna
~~~~~
# pydna
The pydna package.
The pydna package.
:copyright: Copyright 2013 - 2015 by Björn Johansson. All rights reserved.
:license: This code is part of the pydna distribution and governed by its
license. Please see the LICENSE.txt file that should have been included
as part of this package.
:copyright: Copyright 2013 - 2016 by Björn Johansson. All rights reserved.
:license: This code is part of the pydna distribution and governed by its
license. Please see the LICENSE.txt file that should have been included
as part of this package.
'''

Expand All @@ -32,7 +27,7 @@


'''
Pydna caches results from the assembly2 dsdna and amplify
Pydna caches results from the assembly dsdna and amplify
modules. pydna sets an environmental variable "pydna_cache"
which can have three different values:
Expand Down Expand Up @@ -325,3 +320,37 @@ def _open_folder(pth):
from ._version import get_versions
__version__ = get_versions()['version']
del get_versions

class PydnaWarning(Warning):
"""Pydna warning.
Pydna uses this warning (or subclasses of it), to make it easy to
silence all warning messages:
>>> import warnings
>>> from pydna import PydnaWarning
>>> warnings.simplefilter('ignore', PydnaWarning)
Consult the warnings module documentation for more details.
"""
pass


class PydnaDeprecationWarning(PydnaWarning):
"""pydna deprecation warning.
Pydna uses this warning instead of the built in DeprecationWarning
since those are ignored by default since Python 2.7.
To silence all our deprecation warning messages, use:
>>> import warnings
>>> from pydna import PydnaDeprecationWarning
>>> warnings.simplefilter('ignore', PydnaDeprecationWarning)
Code marked as deprecated will be removed in a future version
of Pydna. This can be discussed in the Pydna google group:
https://groups.google.com/forum/#!forum/pydna
"""
pass
4 changes: 2 additions & 2 deletions pydna/_pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class pretty_str(str):
''' Thanks to Min RK, UC Berkeley for this'''
def _repr_pretty_(self, p, cycle):
p.text(self)
def _repr_html_(self):
return "123"
# def _repr_html_(self):
# return "123"

class pretty_unicode(str):
def _repr_pretty_(self, p, cycle):
Expand Down
15 changes: 9 additions & 6 deletions pydna/amplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ class Amplicon(Dseqrecord):

def __init__( self,
record,
*args,
template=None,
forward_primer=None,
reverse_primer=None,
saltc=None,
fprimerc=1000.0,
rprimerc=1000.0,
*args,
**kwargs):

super().__init__(record, *args, **kwargs)
Expand All @@ -199,6 +199,12 @@ def __repr__(self):
'''returns a short string representation of the object'''
return "Amplicon({})".format(self.__len__())

def _repr_pretty_(self, p, cycle):
p.text("Amplicon({})".format(self.__len__()))

def _repr_html_(self):
return "Amplicon({})".format(self.__len__())

def flankup(self, flankuplength=50):
'''Returns a Dseqrecord object containing flankuplength bases upstream of the forward primer footprint,
Truncated if the template is not long enough.
Expand Down Expand Up @@ -829,7 +835,7 @@ def pcr(*args, **kwargs):
elif isinstance(s, str):
s = SeqRecord(Seq(s))
else:
raise TypeError("the record property needs to be a string, a Seq object or a SeqRecord object")
raise TypeError("the record property needs to be a string, Seq, SeqRecord or Dseqrecord object")
new.append(s)

anneal_primers = Anneal( new[:-1],
Expand All @@ -838,10 +844,7 @@ def pcr(*args, **kwargs):

if anneal_primers:
if len(anneal_primers.products) == 1:
result = anneal_primers.products.pop()
msg = "```\n"+result.__repr__()+"```"
display(Markdown(msg))
return result
return anneal_primers.products[0]
elif len(anneal_primers.products) == 0:
raise Exception("No PCR products! {}".format(anneal_primers.report()))
else:
Expand Down
37 changes: 24 additions & 13 deletions pydna/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,23 @@
def display(item): return item
Markdown = display

class Fragment(Dseqrecord):
class _Fragment(Dseqrecord):
'''This class holds information about a DNA fragment in an assembly.
This class is instantiated by the :class:`Assembly` class and is not
meant to be instantiated directly.
'''

def __init__(self, record, start1 = 0,

def __init__(self, record, *args,
start1 = 0,
end1 = 0,
start2 = 0,
end2 = 0,
alignment = 0,
i = 0, *args, **kwargs):
i = 0,
**kwargs):

super(Fragment, self).__init__(record, *args, **kwargs)
super().__init__(record, *args, **kwargs)

self.start1 = start1
self.end1 = end1
Expand All @@ -70,7 +72,7 @@ def __init__(self, record, start1 = 0,
self.i = i

def __str__(self):
return ("Fragment alignment {}\n").format(self.alignment)+super(Fragment, self).__str__()
return ("Fragment alignment {}\n").format(self.alignment)+super().__str__()

class Contig(Dseqrecord):
'''This class holds information about a DNA assembly. This class is instantiated by
Expand All @@ -80,15 +82,24 @@ class Contig(Dseqrecord):

def __init__(self,
record,
*args,
source_fragments=[],
*args, **kwargs):
**kwargs):

super().__init__(record, *args, **kwargs)
self.source_fragments = source_fragments
self.number_of_fragments = len(self.source_fragments)

def __repr__(self):
return "Contig({}{})".format({True:"-", False:"o"}[self.linear],len(self))

def _repr_pretty_(self, p, cycle):
'''returns a short string representation of the object'''
p.text("Contig({}{})".format({True:"-", False:"o"}[self.linear],len(self)))

def _repr_html_(self):
return "<pre>"+self.small_fig()+"</pre>"
#"Contig({}{})".format({True:"-", False:"o"}[self.linear],len(self))

def detailed_figure(self):
'''Synonym of :func:`detailed_fig`'''
Expand Down Expand Up @@ -316,7 +327,7 @@ def __init__(self, dsrecs, limit = 25, only_terminal_overlaps=False, max_nodes=N
setattr(self, key, value )
cache.close()

display(Markdown("```\n"+self.__repr__()+"```".replace('\n', '<br />')))
#display(Markdown("```\n"+self.__repr__()+"```".replace('\n', '<br />')))

def _compare(self, cached):
if str(self) != str(cached):
Expand Down Expand Up @@ -436,7 +447,7 @@ def _assemble(self):
olp2.location.start.position,
olp2.location.end.position,)

source_fragment = Fragment(dsrec,s1,e1,s2,e2,i)
source_fragment = _Fragment(dsrec, start1=s1,end1=e1,start2=s2,end2=e2,i=i) #_Fragment(dsrec,s1,e1,s2,e2,i)

self.G.add_edge( n1, n2,
frag=source_fragment,
Expand Down Expand Up @@ -499,19 +510,19 @@ def _assemble(self):

for pth in all_circular_paths_edges(self.cG):

ns = min(enumerate(pth), key = lambda x:x[1][2]['i'])[0]
ns = min( enumerate(pth), key = lambda x:x[1][2]['i'] )[0]

path = pth[ns:]+pth[:ns]

pred_frag = copy(path[0][2]['frag'])

source_fragments = [pred_frag, ]

if pred_frag.start2<pred_frag.end1:
result=pred_frag[pred_frag.start2+(pred_frag.end1-pred_frag.start2):pred_frag.end2]
else:
result=pred_frag[pred_frag.end1:pred_frag.end2]

result.seq = Dseq(str(result.seq))

for first_node, second_node, edgedict in path[1:]:
Expand Down Expand Up @@ -542,7 +553,7 @@ def _assemble(self):
# circular_products[len(result)].append( Contig( Dseqrecord(result, circular=True), source_fragments))

r = Dseqrecord(result, circular=True)
circular_products[r.cseguid()] = Contig(r, source_fragments )
circular_products[r.cseguid()] = Contig(r, source_fragments = source_fragments )


#self.circular_products = list(itertools.chain.from_iterable(circular_products[size] for size in sorted(circular_products, reverse=True)))
Expand Down
Loading

0 comments on commit 3895997

Please sign in to comment.