Package Bio :: Package Wise :: Module psw
[hide private]
[frames] | no frames]

Source Code for Module Bio.Wise.psw

  1  #!/usr/bin/env python 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  __version__ = "$Revision: 1.4 $" 
  7   
  8  import exceptions 
  9  import os 
 10  import re 
 11  import sys 
 12   
 13  from Bio import Wise 
 14   
 15  _CMDLINE_PSW = ["psw", "-l", "-F"] 
 16  _OPTION_GAP_START = "-g" 
 17  _OPTION_GAP_EXTENSION = "-e" 
 18  _OPTION_SCORES = "-m" 
 19   
20 -class AlignmentColumnFullException(exceptions.Exception):
21 pass
22
23 -class Alignment(list):
24 - def append(self, column_unit):
25 try: 26 self[-1].append(column_unit) 27 except AlignmentColumnFullException: 28 list.append(self, AlignmentColumn(column_unit)) 29 except IndexError: 30 list.append(self, AlignmentColumn(column_unit))
31
32 -class AlignmentColumn(list):
33 - def _set_kind(self, column_unit):
34 if self.kind == "SEQUENCE": 35 self.kind = column_unit.kind
36
37 - def __init__(self, column_unit):
38 assert column_unit.unit == 0 39 self.kind = column_unit.kind 40 list.__init__(self, [column_unit.column, None])
41
42 - def __repr__(self):
43 return "%s(%s, %s)" % (self.kind, self[0], self[1])
44
45 - def append(self, column_unit):
46 if self[1] is not None: 47 raise AlignmentColumnFullException 48 49 assert column_unit.unit == 1 50 51 self._set_kind(column_unit) 52 self[1] = column_unit.column
53
54 -class ColumnUnit(object):
55 - def __init__(self, unit, column, kind):
56 self.unit = unit 57 self.column = column 58 self.kind = kind
59
60 - def __str__(self):
61 return "ColumnUnit(unit=%s, column=%s, %s)" % (self.unit, self.column, self.kind)
62 63 __repr__ = __str__
64 65 _re_unit = re.compile(r"^Unit +([01])- \[ *(-?\d+)- *(-?\d+)\] \[(\w+)\]$")
66 -def parse_line(line):
67 """ 68 >>> print parse_line("Column 0:") 69 None 70 >>> parse_line("Unit 0- [ -1- 0] [SEQUENCE]") 71 ColumnUnit(unit=0, column=0, SEQUENCE) 72 >>> parse_line("Unit 1- [ 85- 86] [SEQUENCE]") 73 ColumnUnit(unit=1, column=86, SEQUENCE) 74 """ 75 match = _re_unit.match(line.rstrip()) 76 77 if not match: 78 return 79 80 return ColumnUnit(int(match.group(1)), int(match.group(3)), match.group(4))
81
82 -def parse(iterable):
83 """ 84 format 85 86 Column 0: 87 Unit 0- [ -1- 0] [SEQUENCE] 88 Unit 1- [ 85- 86] [SEQUENCE] 89 90 means that seq1[0] == seq2[86] (0-based) 91 """ 92 93 alignment = Alignment() 94 for line in iterable: 95 try: 96 if os.environ["WISE_PY_DEBUG"]: 97 print line, 98 except KeyError: 99 pass 100 101 column_unit = parse_line(line) 102 if column_unit: 103 alignment.append(column_unit) 104 105 return alignment
106
107 -def align(pair, 108 scores=None, 109 gap_start=None, 110 gap_extension=None, 111 *args, **keywds):
112 113 cmdline = _CMDLINE_PSW[:] 114 if scores: 115 cmdline.extend((_OPTION_SCORES, scores)) 116 if gap_start: 117 cmdline.extend((_OPTION_GAP_START, str(gap_start))) 118 if gap_extension: 119 cmdline.extend((_OPTION_GAP_EXTENSION, str(gap_extension))) 120 temp_file = Wise.align(cmdline, pair, *args, **keywds) 121 return parse(temp_file)
122
123 -def main():
124 print align(sys.argv[1:3])
125
126 -def _test(*args, **keywds):
127 import doctest, sys 128 doctest.testmod(sys.modules[__name__], *args, **keywds)
129 130 if __name__ == "__main__": 131 if __debug__: 132 _test() 133 main() 134