使用python实现BLAST

yipeiwu_com7年前Python基础

最近在自学python,又用python实现了一下BLAST。

这次更新了打分函数如下,空位罚分改为-5,但不区分gap open 和 gap extend。

''''' 
@author: JiuYu 
''' 
 
def score(a,b):#scoring function 
  score=0 
  lst=['AC','GT','CA','TG'] 
  if a==b: 
    score +=2 
  elif a+b in lst: 
    score += -5 
  else: 
    score += -7 
  return score 
 
def BLAST(seq1,seq2):#Basic Local Alignment Search Tool 
  l1 = len(seq1) 
  l2 = len(seq2) 
  GAP =-5   #-5 for any gap 
  scores =[] 
  point =[] 
   
  for j in range(l2+1): 
    if j == 0: 
      line1=[0] 
      line2=[0] 
      for i in range(1,l1+1): 
        line1.append(GAP*i) 
        line2.append(2) 
    else: 
      line1=[] 
      line2=[] 
      line1.append(GAP*j) 
      line2.append(3) 
    scores.append(line1) 
    point.append(line2) 
   
  #fill the blank of scores and point 
  for j in range(1,l2+1): 
    letter2 = seq2[j-1] 
    for i in range(1,l1+1): 
      letter1 = seq1[i-1] 
      diagonal_score = score(letter1, letter2) + scores[j-1][i-1] 
      left_score = GAP + scores[j][i-1] 
      up_score = GAP + scores[j-1][i] 
      max_score = max(diagonal_score, left_score, up_score) 
      scores[j].append(max_score) 
       
      if scores[j][i] == diagonal_score: 
        point[j].append(1) 
      elif scores[j][i] == left_score: 
        point[j].append(2) 
      else: 
        point[j].append(3) 
         
  #trace back 
  alignment1='' 
  alignment2='' 
  i = l2 
  j = l1 
  print 'scores =',scores[i][j] 
  while True: 
    if point[i][j] == 0: 
      break 
    elif point[i][j] == 1: 
      alignment1 += seq1[j-1] 
      alignment2 += seq2[i-1] 
      i -= 1 
      j -= 1 
    elif point[i][j] == 2: 
      alignment1 += seq1[j-1] 
      alignment2 += '-' 
      j -= 1 
    else: 
      alignment1 += '-' 
      alignment2 += seq2[i-1] 
      i -= 1 
       
  #reverse alignment 
  alignment1 = alignment1[::-1] 
  alignment2 = alignment2[::-1] 
  print 'The best alignment:' 
  print alignment1 
  print alignment2 
 
seq1=raw_input('Please input your first sequences:\n') 
seq2=raw_input('input second sequences:\n') 
BLAST(seq1, seq2) 

运行结果:

无疑python对字符串的处理更加强大,语言也更加简单,优雅。比如最后逆序输出alignment,java我是单独写了一个逆序函数,而python只用一个语句就可以完成相同任务。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python之Flask实现简单登录功能的示例代码

python之Flask实现简单登录功能的示例代码

网站少不了要和数据库打交道,归根到底都是一些增删改查操作,这里做一个简单的用户登录功能来学习一下Flask如何操作MySQL。 用到的一些知识点:Flask-SQLAlchemy、Fla...

Python多进程分块读取超大文件的方法

本文实例讲述了Python多进程分块读取超大文件的方法。分享给大家供大家参考,具体如下: 读取超大的文本文件,使用多进程分块读取,将每一块单独输出成文件 # -*- coding:...

Python中统计函数运行耗时的方法

本文实例讲述了Python中统计函数运行耗时的方法。分享给大家供大家参考。具体实现方法如下: import time def time_me(fn): def _wrapper(...

一文带你了解Python中的字符串是什么

一文带你了解Python中的字符串是什么

在《 详解Python拼接字符串的七种方式 》这篇文章里,我提到过,字符串是程序员离不开的事情。后来,我看到了一个英文版本的说法: There are few guarantees in...

Python中实现输入超时及如何通过变量获取变量名

Python中实现输入超时及如何通过变量获取变量名

背景介绍 开发中遇到了一个需求:程序运行到某处时需要用户确认, 但不能一直傻等, 后面的程序不能被一直阻塞, 需要有个超时限制, 也就是这个程序如果在一段时间后还没有得到用户输入就执行...