Skip to content

Instantly share code, notes, and snippets.

@pratikone
Last active October 25, 2016 00:35
Show Gist options
  • Save pratikone/cc0a4d35961567a63a7f4bd45c18ab6e to your computer and use it in GitHub Desktop.
Save pratikone/cc0a4d35961567a63a7f4bd45c18ab6e to your computer and use it in GitHub Desktop.
Global and local alignments
##########################
# Author : Pratik Anand #
##########################
from tabulate import tabulate
import copy
IS_LOCAL_ALIGNMENT = False
USE_BLOSUM = False
match = 1
mismatch = -1
MIN = -1000
def printTable( rowHeader, columnHeader, table ) :
new_table = copy.deepcopy(table)
for indx,innerRow in enumerate(new_table):
innerRow.insert( 0, rowHeader[indx])
print tabulate( new_table, headers=columnHeader, tablefmt="grid" )
def sequence_align( strA, strB, charSet= None,blosum = None) :
print strA + " vS " + strB
strA = "0" + strA
strB = "0" + strB
megaL = []
for i in range(len(strB)):
innerL = [0 for x in range(len(strA))] # list assignment evil
megaL.append(innerL)
for i in range( len(strB) ) :
for j in range( len(strA) ) :
if i==0 and j==0 :
megaL[i][j] = 0
continue
indx_i = -1
indx_j = -1
if USE_BLOSUM is True :
listCharSet = list(charSet)
for indx,string in enumerate(listCharSet) :
if string == strB[i] :
indx_i = indx
if string == strA[j]:
indx_j = indx
local_matched = (match if strB[i] == strA[j] else mismatch) if USE_BLOSUM is False else (blosum[indx_i][indx_j] if blosum[indx_i][indx_j] != "?" else MIN )
megaL[i][j] = max( ( megaL[i-1][j-1] if (i-1) >=0 and (j-1) >=0 else MIN ) + local_matched,
( megaL[i-1][j] if (i-1) >=0 else MIN ) + mismatch,
( megaL[i][j-1] if (j-1) >=0 else MIN ) + mismatch,
0 if IS_LOCAL_ALIGNMENT is True else MIN
)
#print-sandra
for i in range( len(strB) ) :
for j in range( len(strA) ) :
print str(megaL[i][j]) + " ",
print "\n"
printTable(strB, list(strA), megaL)
if __name__ == '__main__' :
s1 = "TACGGGTAT"
s2 = "GGACGTACG"
sequence_align(s1, s2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment