Package Bio :: Package CAPS
[hide private]
[frames] | no frames]

Source Code for Package Bio.CAPS

  1  # Copyright 2005 by Jonathan Taylor. 
  2  # All rights reserved. 
  3  # This code is part of the Biopython distribution and governed by its 
  4  # license.  Please see the LICENSE file that should have been included 
  5  # as part of this package. 
  6  """This module deals with CAPS markers. 
  7   
  8  A CAPS marker is a location a DifferentialCutsite as described below and a 
  9  set of primers that can be used to visualize this.  More information can 
 10  be found in the paper `Konieczny and Ausubel (1993)`_ (PMID 8106085). 
 11   
 12  .. _`Konieczny and Ausubel (1993)`: http://dx.doi.org/10.1046/j.1365-313X.1993.04020403.x 
 13  """ 
 14   
 15  __docformat__ = "restructuredtext en" 
 16   
 17   
18 -class DifferentialCutsite(object):
19 """Differential enzyme cutsite in an alignment. 20 21 A differential cutsite is a location in an alignment where an enzyme cuts 22 at least one sequence and also cannot cut at least one other sequence. 23 24 Members: 25 - start - Where it lives in the alignment. 26 - enzyme - The enzyme that causes this. 27 - cuts_in - A list of sequences (as indexes into the alignment) the 28 enzyme cuts in. 29 - blocked_in - A list of sequences (as indexes into the alignment) the 30 enzyme is blocked in. 31 32 """ 33
34 - def __init__(self, **kwds):
35 """Initialize a DifferentialCutsite. 36 37 Each member (as listed in the class description) should be included as a 38 keyword. 39 """ 40 41 self.start = int(kwds["start"]) 42 self.enzyme = kwds["enzyme"] 43 self.cuts_in = kwds["cuts_in"] 44 self.blocked_in = kwds["blocked_in"]
45 46
47 -class AlignmentHasDifferentLengthsError(Exception):
48 pass
49 50
51 -class CAPSMap(object):
52 """A map of an alignment showing all possible dcuts. 53 54 Members: 55 - alignment - The alignment that is mapped. 56 - dcuts - A list of possible CAPS markers in the form of 57 DifferentialCutsites. 58 """ 59
60 - def __init__(self, alignment, enzymes=None):
61 """Initialize the CAPSMap. 62 63 Required: 64 - alignment - The alignment to be mapped. 65 66 Optional: 67 - enzymes - List of enzymes to be used to create the map. 68 Defaults to an empty list. 69 """ 70 if enzymes is None: 71 enzymes = [] 72 self.sequences = [rec.seq for rec in alignment] 73 self.size = len(self.sequences) 74 self.length = len(self.sequences[0]) 75 for seq in self.sequences: 76 if len(seq) != self.length: 77 raise AlignmentHasDifferentLengthsError 78 79 self.alignment = alignment 80 self.enzymes = enzymes 81 82 # look for dcuts 83 self._digest()
84
85 - def _digest_with(self, enzyme):
86 cuts = [] # list of lists, one per sequence 87 all = [] 88 89 # go through each sequence 90 for seq in self.sequences: 91 # grab all the cuts in the sequence 92 seq_cuts = [cut - enzyme.fst5 for cut in enzyme.search(seq)] 93 # maintain a list of all cuts in all sequences 94 all.extend(seq_cuts) 95 cuts.append(seq_cuts) 96 97 # we sort the all list and remove duplicates 98 all.sort() 99 100 last = -999 101 new = [] 102 for cut in all: 103 if cut != last: 104 new.append(cut) 105 last = cut 106 all = new 107 # all now has indices for all sequences in the alignment 108 109 for cut in all: 110 # test for dcuts 111 112 cuts_in = [] 113 blocked_in = [] 114 115 for i in range(0, self.size): 116 seq = self.sequences[i] 117 if cut in cuts[i]: 118 cuts_in.append(i) 119 else: 120 blocked_in.append(i) 121 122 if cuts_in != [] and blocked_in != []: 123 self.dcuts.append(DifferentialCutsite(start=cut, 124 enzyme=enzyme, 125 cuts_in=cuts_in, 126 blocked_in=blocked_in))
127
128 - def _digest(self):
129 self.dcuts = [] 130 131 for enzyme in self.enzymes: 132 self._digest_with(enzyme)
133