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