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