forked from bmajoros/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmithWaterman.py
executable file
·67 lines (59 loc) · 2.56 KB
/
SmithWaterman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
#=========================================================================
# This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
# License (GPL) version 3, as described at www.opensource.org.
# Author: William H. Majoros ([email protected])
#=========================================================================
from __future__ import (absolute_import, division, print_function,
unicode_literals, generators, nested_scopes, with_statement)
from builtins import (bytes, dict, int, list, object, range, str, ascii,
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
# The above imports should allow this program to run in both Python 2 and
# Python 3. You might need to update your version of module "future".
import os
from Pipe import Pipe
from Rex import Rex
rex=Rex()
import TempFilename
from CigarString import CigarString
from FastaWriter import FastaWriter
#=========================================================================
# Attributes:
#
# Instance Methods:
# aligner=SmithWaterman(alignerDir,matrixFile,openPenalty,extrendPenalty)
# cigarString=aligner.align(seq1,seq2)
#=========================================================================
class SmithWaterman:
def __init__(self,alignerDir,matrixFile,gapOpenPenalty,gapExtendPenalty):
self.alignerDir=alignerDir
self.matrixFile=matrixFile
self.gapOpen=gapOpenPenalty
self.gapExtend=gapExtendPenalty
self.fastaWriter=FastaWriter()
def writeFile(self,defline,seq):
filename=TempFilename.generate("fasta")
self.fastaWriter.writeFasta(defline,seq,filename)
return filename
def swapInsDel(self,cigar):
# This is done because my aligner defines insertions and deletions
# opposite to how they're defined in the SAM specification
newCigar=""
for x in cigar:
if(x=="I"): x="D"
elif(x=="D"): x="I"
newCigar+=x
return newCigar
def align(self,seq1,seq2):
file1=self.writeFile("query",seq1)
file2=self.writeFile("reference",seq2)
cmd=self.alignerDir+"/smith-waterman -q "+self.matrixFile+" "+\
str(self.gapOpen)+" "+str(self.gapExtend)+" "+file1+" "+file2+" DNA"
output=Pipe.run(cmd)
os.remove(file1)
os.remove(file2)
if(not rex.find("CIGAR=(\S+)",output)):
raise Exception("Can't parse aligner output: "+output)
cigar=rex[1]
cigar=self.swapInsDel(cigar) # because I define cigars differently
return CigarString(cigar)