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  # Bio.Wise contains modules for running and processing the output of 
  7  # some of the models in the Wise2 package by Ewan Birney available from: 
  8  # ftp://ftp.ebi.ac.uk/pub/software/unix/wise2/ 
  9  # http://www.ebi.ac.uk/Wise2/ 
 10  #  
 11  # Bio.Wise.psw is for protein Smith-Waterman alignments 
 12  # Bio.Wise.dnal is for Smith-Waterman DNA alignments 
 13   
 14  __version__ = "$Revision: 1.5 $" 
 15   
 16  import exceptions 
 17  import os 
 18  import re 
 19  import sys 
 20   
 21  from Bio import Wise 
 22   
 23  _CMDLINE_PSW = ["psw", "-l", "-F"] 
 24  _OPTION_GAP_START = "-g" 
 25  _OPTION_GAP_EXTENSION = "-e" 
 26  _OPTION_SCORES = "-m" 
 27   
28 -class AlignmentColumnFullException(exceptions.Exception):
29 pass
30
31 -class Alignment(list):
32 - def append(self, column_unit):
33 try: 34 self[-1].append(column_unit) 35 except AlignmentColumnFullException: 36 list.append(self, AlignmentColumn(column_unit)) 37 except IndexError: 38 list.append(self, AlignmentColumn(column_unit))
39
40 -class AlignmentColumn(list):
41 - def _set_kind(self, column_unit):
42 if self.kind == "SEQUENCE": 43 self.kind = column_unit.kind
44
45 - def __init__(self, column_unit):
46 assert column_unit.unit == 0 47 self.kind = column_unit.kind 48 list.__init__(self, [column_unit.column, None])
49
50 - def __repr__(self):
51 return "%s(%s, %s)" % (self.kind, self[0], self[1])
52
53 - def append(self, column_unit):
54 if self[1] is not None: 55 raise AlignmentColumnFullException 56 57 assert column_unit.unit == 1 58 59 self._set_kind(column_unit) 60 self[1] = column_unit.column
61
62 -class ColumnUnit(object):
63 - def __init__(self, unit, column, kind):
64 self.unit = unit 65 self.column = column 66 self.kind = kind
67
68 - def __str__(self):
69 return "ColumnUnit(unit=%s, column=%s, %s)" % (self.unit, self.column, self.kind)
70 71 __repr__ = __str__
72 73 _re_unit = re.compile(r"^Unit +([01])- \[ *(-?\d+)- *(-?\d+)\] \[(\w+)\]$")
74 -def parse_line(line):
75 """ 76 >>> print parse_line("Column 0:") 77 None 78 >>> parse_line("Unit 0- [ -1- 0] [SEQUENCE]") 79 ColumnUnit(unit=0, column=0, SEQUENCE) 80 >>> parse_line("Unit 1- [ 85- 86] [SEQUENCE]") 81 ColumnUnit(unit=1, column=86, SEQUENCE) 82 """ 83 match = _re_unit.match(line.rstrip()) 84 85 if not match: 86 return 87 88 return ColumnUnit(int(match.group(1)), int(match.group(3)), match.group(4))
89
90 -def parse(iterable):
91 """ 92 format 93 94 Column 0: 95 Unit 0- [ -1- 0] [SEQUENCE] 96 Unit 1- [ 85- 86] [SEQUENCE] 97 98 means that seq1[0] == seq2[86] (0-based) 99 """ 100 101 alignment = Alignment() 102 for line in iterable: 103 try: 104 if os.environ["WISE_PY_DEBUG"]: 105 print line, 106 except KeyError: 107 pass 108 109 column_unit = parse_line(line) 110 if column_unit: 111 alignment.append(column_unit) 112 113 return alignment
114
115 -def align(pair, 116 scores=None, 117 gap_start=None, 118 gap_extension=None, 119 *args, **keywds):
120 121 cmdline = _CMDLINE_PSW[:] 122 if scores: 123 cmdline.extend((_OPTION_SCORES, scores)) 124 if gap_start: 125 cmdline.extend((_OPTION_GAP_START, str(gap_start))) 126 if gap_extension: 127 cmdline.extend((_OPTION_GAP_EXTENSION, str(gap_extension))) 128 temp_file = Wise.align(cmdline, pair, *args, **keywds) 129 return parse(temp_file)
130
131 -def main():
132 print align(sys.argv[1:3])
133
134 -def _test(*args, **keywds):
135 import doctest, sys 136 doctest.testmod(sys.modules[__name__], *args, **keywds)
137 138 if __name__ == "__main__": 139 if __debug__: 140 _test() 141 main() 142