序列比对算法-计算生物学

2023-11-13

1.序列比对指将两个或多个序列排列在一起,标明其相似之处。序列中可以插入间隔(通常用短横线“-”表示)。对应的相同或相似的符号(在核酸中是A, T(或U), C, G,在蛋白质中是氨基酸残基的单字母表示)排列在同一列上。这一方法常用于研究由共同祖先进化而来的序列,特别是如蛋白质序列或DNA序列等生物序列。进行相似度分析。
这里写图片描述

2.分类

  • 全局比对:将两个序列中的所有字符都进行依次比对,由于各方面的缺陷,可用性不强。
  • 局部比对(local alignment):通过动态规划的方式,改动最少来匹配两个序列最相似的部分。
  • 双序列比对:只需要比对两个序列
  • 多序列比对:基于双序列比对,这样,主要是用来提取多个不同序列中,具有的共同特征信息。

3.双序列局部比对。

  • 最大子序列问题,对于两个字符串,通过比较找到两个字符串中相同的子序列,不考虑间隔。
/**
 1. Design an algorithm to find the Longest Common Subsequence,
 Longest Common Substring between two sequences.
 * Created by zoutai on 2017/10/1.
 * 从后向前遍历查找,迭代/遍历+动态规划
 * 但是递归的方式有一个坏处,就是内部往往那个存在很多重复计算,最大时间复杂度2^n
 * 所以我们采用空间换时间的办法:直接将所有的L[i][j]遍历出来,时间复杂度即为(mn)
 */
public class LongestCommonSubsequence {
    public static void main(String[] args) {
        String s1 = "AGGTAB";
        String s2 = "GXTXAYB";
        int m = s1.length(),n=s2.length();
        String result = getLongestSubStr(s1,s2,m,n);
        System.out.println(result);
    }

    // 从后向前遍历
    public static String getLongestSubStr(String s1,String s2,int m,int n) {
        if (m == 0 || n == 0)
            return "";
        if (s1.charAt(m - 1) == s2.charAt(n - 1))
            return getLongestSubStr(s1, s2, m - 1, n - 1)+s1.charAt(m - 1);
        else
            return max(getLongestSubStr(s1, s2, m, n - 1), getLongestSubStr(s1, s2, m - 1, n));
    }

    // 比较字符串长度
    public static String max(String a, String b) {
        if(a==null||b==null){
            return "";
        }
        return (a.length() > b.length()) ? a : b;
    }
}
  • Smith-Waterman算法:
    算法主要思想:
    对于A,B两个序列,n,m分别表示他们的长度,构造相应的矩阵和罚分;通过罚分矩阵,得到分数最大的一条路径,即为做好的匹配路径。
    参考链接:wiki
    这里写图片描述
    实现方式:
    • 普通空白函数:
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zoutai on 2017/10/2.
 * 2.Tandem repeats. Let P be a pattern of length n and T a text of length m.
 * Let Pm be the concatenation of P with itself m times, so Pm has length mn.
 * We want to compute a local alignment between Pm and T .
 */

public class Alignment02 {
    public static void main(String[] args) {
        List<String> list = new LinkedList<String>();

        /* 样例1:
        输出为:
            G-CTAGCT
            GACTA-CT
         */
        String P = new String("AGCT");
        String T = new String("GACTACT");
        String Pm = new String();
        Pm = pToPm(P,T);
        list = localAlig(Pm,T);

        /* (不复制)样例2:
        输出为:
            GTT-AC
            GTTGAC
         */
//      String t1 = new String("TGTTACGG");
//      String t2 = new String("GGTTGACTA");
//        list = localAlig(t1, t2);

        for (String str : list) {
            System.out.println(str.toString());
        }
    }

    private static List<String> localAlig(String A, String B) {
        List<String> twoList = new LinkedList<String>();
        int allMax = 0;
        int col = A.length();
        int row = B.length();
        int[][] mat = new int[row + 1][col + 1];
        for (int i = 0; i < col + 1; i++) {
            mat[0][i] = 0;
        }
        for (int j = 0; j < row + 1; j++) {
            mat[j][0] = 0;
        }
        for (int i = 1; i < col+1; i++) {
            for (int j = 1; j < row+1; j++) {
                int Match = mat[j - 1][i - 1] + (B.charAt(j-1)==A.charAt(i-1)?3:-3);
                int Delete = mat[j - 1][i] + (0 - 2);
                int Insert = mat[j][i - 1] + (0 - 2);
                mat[j][i] = Math.max(Math.max(Match,0), Math.max(Insert, Delete));
            }
        }

        // 再次遍历矩阵,获取矩阵最大值,用于查找序列初始点
        int tempMax = 0;
        int temprow =0,tempcol = 0;
        for (int i =0;i<row+1;i++) {
            for (int j =0;j<col+1;j++) {
                System.out.print(mat[i][j]+".");
                if (tempMax < mat[i][j]) {
                    tempMax = mat[i][j];
                    temprow = i;
                    tempcol = j;
                }
            }
            System.out.println();
        }

        String AlignmentA = "";
        String AlignmentB = "";
        int i = tempcol;
        int j = temprow;
        while (i > 0 && j > 0) {
            if (i > 0 && j > 0 && mat[j][i] == mat[j-1][i-1]
                    + (B.charAt(j-1)==A.charAt(i-1)?3:-3)) {
                AlignmentA = A.charAt(i - 1) + AlignmentA;
                AlignmentB = B.charAt(j - 1) + AlignmentB;
                i = i - 1;
                j = j - 1;
            } else if (i == 1 || j == 1) {
                break;
            } else if (j > 0 && mat[j][i] == mat[j - 1][i] + (0 - 2)) {
                AlignmentA = "-" + AlignmentA;
                AlignmentB = B.charAt(j - 1) + AlignmentB;
                j = j - 1;
            } else {
                AlignmentA = A.charAt(i - 1) + AlignmentA;
                AlignmentB = "-" + AlignmentB;
                i = i - 1;
            }
        }
        twoList.add(AlignmentA);
        twoList.add(AlignmentB);
        return twoList;
    }

    // 将p字符串转为pm
    private static String pToPm(String p, String t) {
        // TODO Auto-generated method stub
        int n = p.length();
        int m = t.length();
        StringBuffer sb = new StringBuffer();
        for (int i=0;i<m;i++) {
            sb.append(p);
        }
        return sb.toString();
    }
}
* 线性空白函数:
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zoutai on 2017/10/2.
 * Implementation of simple local alignment algorithm
 *
 * 线性罚分,Ws=2;Wd=0;
 */
public class Alignment0301 {
    public static final int[][] PAM250 = {
            {4, -1, -2, -2, 0, -1, -1, 0, -2, -1, -1, -1, -1, -2, -1, 1, 0, -3, -2, 0},
            {-1, 5, 0, -2, -3, 1, 0, -2, 0, -3, -2, 2, -1, -3, -2, -1, -1, -3, -2, -3},
            {-2, 0, 6, 1, -3, 0, 0, 0, 1, -3, -3, 0, -2, -3, -2, 1, 0, -4, -2, -3},
            {-2, -2, 1, 6, -3, 0, 2, -1, -1, -3, -4, -1, -3, -3, -1, 0, -1, -4, -3, -3},
            {0, -3, -3, -3, 9, -3, -4, -3, -3, -1, -1, -3, -1, -2, -3, -1, -1, -2, -2, -1},
            {-1, 1, 0, 0, -3, 5, 2, -2, 0, -3, -2, 1, 0, -3, -1, 0, -1, -2, -1, -2},
            {-1, 0, 0, 2, -4, 2, 5, -2, 0, -3, -3, 1, -2, -3, -1, 0, -1, -3, -2, -2},
            {0, -2, 0, -1, -3, -2, -2, 6, -2, -4, -4, -2, -3, -3, -2, 0, -2, -2, -3, -3},
            {-2, 0, 1, -1, -3, 0, 0, -2, 8, -3, -3, -1, -2, -1, -2, -1, -2, -2, 2, -3},
            {-1, -3, -3, -3, -1, -3, -3, -4, -3, 4, 2, -3, 1, 0, -3, -2, -1, -3, -1, 3},
            {-1, -2, -3, -4, -1, -2, -3, -4, -3, 2, 4, -2, 2, 0, -3, -2, -1, -2, -1, 1},
            {-1, 2, 0, -1, -3, 1, 1, -2, -1, -3, -2, 5, -1, -3, -1, 0, -1, -3, -2, -2},
            {-1, -1, -2, -3, -1, 0, -2, -3, -2, 1, 2, -1, 5, 0, -2, -1, -1, -1, -1, 1},
            {-2, -3, -3, -3, -2, -3, -3, -3, -1, 0, 0, -3, 0, 6, -4, -2, -2, 1, 3, -1},
            {-1, -2, -2, -1, -3, -1, -1, -2, -2, -3, -3, -1, -2, -4, 7, -1, -1, -4, -3, -2},
            {1, -1, 1, 0, -1, 0, 0, 0, -1, -2, -2, 0, -1, -2, -1, 4, 1, -3, -2, -2},
            {0, -1, 0, -1, -1, -1, -1, -2, -2, -1, -1, -1, -1, -2, -1, 1, 5, -2, -2, 0},
            {-3, -3, -4, -4, -2, -2, -3, -2, -2, -3, -2, -3, -1, 1, -4, -3, -2, 11, 2, -3},
            {-2, -2, -2, -3, -2, -1, -2, -3, 2, -1, -1, -2, -1, 3, -3, -2, -2, 2, 7, -1},
            {0, -3, -3, -3, -1, -2, -2, -3, -3, 3, 1, -2, 1, -1, -2, -2, 0, -3, -1, 4}};

    public static void main(String[] args) {
        String t1 = new String("TGTTACGG");
        String t2 = new String("GGTTGACTA");
        List<String> list = new LinkedList<String>();
        list = localAlig(t1, t2);
        for (String str : list) {
            System.out.println(str.toString());
        }
    }

    private static List<String> localAlig(String A, String B) {
        List<String> twoList = new LinkedList<String>();
        int allMax = 0;
        int col = A.length();
        int row = B.length();
        int[][] mat = new int[row + 1][col + 1];
        for (int i = 0; i < col + 1; i++) {
            mat[0][i] = 0;
        }
        for (int j = 0; j < row + 1; j++) {
            mat[j][0] = 0;
        }
        for (int i = 1; i < col+1; i++) {
            for (int j = 1; j < row+1; j++) {
                int Match = mat[j - 1][i - 1] + (B.charAt(j-1)==A.charAt(i-1)?3:-3);
                int Delete = mat[j - 1][i] + (0 - 2);
                int Insert = mat[j][i - 1] + (0 - 2);
                mat[j][i] = Math.max(Math.max(Match,0), Math.max(Insert, Delete));
            }
        }

        // 再次遍历矩阵,获取矩阵最大值,用于查找序列初始点
        int tempMax = 0;
        int temprow =0,tempcol = 0;
        for (int i =0;i<row+1;i++) {
            for (int j =0;j<col+1;j++) {
                System.out.print(mat[i][j]+".");
                if (tempMax < mat[i][j]) {
                    tempMax = mat[i][j];
                    temprow = i;
                    tempcol = j;
                }
            }
            System.out.println();
        }

        String AlignmentA = "";
        String AlignmentB = "";
        int i = tempcol;
        int j = temprow;
        while (i > 1 && j > 1) {
            if (i > 1 && j > 1 && mat[j][i] == mat[j-1][i-1]
                    + (B.charAt(j-1)==A.charAt(i-1)?3:-3)) {
                AlignmentA = A.charAt(i - 1) + AlignmentA;
                AlignmentB = B.charAt(j - 1) + AlignmentB;
                i = i - 1;
                j = j - 1;
            } else if (i == 1 || j == 1) {
                break;
            } else if (j > 1 && mat[j][i] == mat[j - 1][i] + (0 - 2)) {
                AlignmentA = "-" + AlignmentA;
                AlignmentB = B.charAt(j - 1) + AlignmentB;
                j = j - 1;
            } else {
                AlignmentA = A.charAt(i - 1) + AlignmentA;
                AlignmentB = "-" + AlignmentB;
                i = i - 1;
            }
        }
        twoList.add(AlignmentA);
        twoList.add(AlignmentB);
        return twoList;
    }
}
* Affine空白函数:
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zoutai on 2017/10/2.
 * Implementation the classical affine-gap local alignment algorithm
 *
 * Affine罚分,Ws=2;Wd=10;
 */
public class Alignment0302 {

    public static final int[][] PAM250 = {
            {4, -1, -2, -2, 0, -1, -1, 0, -2, -1, -1, -1, -1, -2, -1, 1, 0, -3, -2, 0},
            {-1, 5, 0, -2, -3, 1, 0, -2, 0, -3, -2, 2, -1, -3, -2, -1, -1, -3, -2, -3},
            {-2, 0, 6, 1, -3, 0, 0, 0, 1, -3, -3, 0, -2, -3, -2, 1, 0, -4, -2, -3},
            {-2, -2, 1, 6, -3, 0, 2, -1, -1, -3, -4, -1, -3, -3, -1, 0, -1, -4, -3, -3},
            {0, -3, -3, -3, 9, -3, -4, -3, -3, -1, -1, -3, -1, -2, -3, -1, -1, -2, -2, -1},
            {-1, 1, 0, 0, -3, 5, 2, -2, 0, -3, -2, 1, 0, -3, -1, 0, -1, -2, -1, -2},
            {-1, 0, 0, 2, -4, 2, 5, -2, 0, -3, -3, 1, -2, -3, -1, 0, -1, -3, -2, -2},
            {0, -2, 0, -1, -3, -2, -2, 6, -2, -4, -4, -2, -3, -3, -2, 0, -2, -2, -3, -3},
            {-2, 0, 1, -1, -3, 0, 0, -2, 8, -3, -3, -1, -2, -1, -2, -1, -2, -2, 2, -3},
            {-1, -3, -3, -3, -1, -3, -3, -4, -3, 4, 2, -3, 1, 0, -3, -2, -1, -3, -1, 3},
            {-1, -2, -3, -4, -1, -2, -3, -4, -3, 2, 4, -2, 2, 0, -3, -2, -1, -2, -1, 1},
            {-1, 2, 0, -1, -3, 1, 1, -2, -1, -3, -2, 5, -1, -3, -1, 0, -1, -3, -2, -2},
            {-1, -1, -2, -3, -1, 0, -2, -3, -2, 1, 2, -1, 5, 0, -2, -1, -1, -1, -1, 1},
            {-2, -3, -3, -3, -2, -3, -3, -3, -1, 0, 0, -3, 0, 6, -4, -2, -2, 1, 3, -1},
            {-1, -2, -2, -1, -3, -1, -1, -2, -2, -3, -3, -1, -2, -4, 7, -1, -1, -4, -3, -2},
            {1, -1, 1, 0, -1, 0, 0, 0, -1, -2, -2, 0, -1, -2, -1, 4, 1, -3, -2, -2},
            {0, -1, 0, -1, -1, -1, -1, -2, -2, -1, -1, -1, -1, -2, -1, 1, 5, -2, -2, 0},
            {-3, -3, -4, -4, -2, -2, -3, -2, -2, -3, -2, -3, -1, 1, -4, -3, -2, 11, 2, -3},
            {-2, -2, -2, -3, -2, -1, -2, -3, 2, -1, -1, -2, -1, 3, -3, -2, -2, 2, 7, -1},
            {0, -3, -3, -3, -1, -2, -2, -3, -3, 3, 1, -2, 1, -1, -2, -2, 0, -3, -1, 4}};

    public static void main(String[] args) {
        String t1 = new String("TGTTACGG");
        String t2 = new String("GGTTGACTA");
        List<String> list = new LinkedList<String>();

        list = localAlig(t1, t2);

        for (String str : list) {
            System.out.println(str.toString());
        }
    }

    private static List<String> localAlig(String A, String B) {
        List<String> twoList = new LinkedList<String>();
        int allMax = 0;
        int col = A.length();
        int row = B.length();
        int[][] mat = new int[row + 1][col + 1];
        for (int i = 0; i < col + 1; i++) {
            mat[0][i] = -10;
        }
        for (int j = 0; j < row + 1; j++) {
            mat[j][0] = -10;
        }
        for (int i = 1; i < col+1; i++) {
            for (int j = 1; j < row+1; j++) {
                int Match = mat[j - 1][i - 1] + Scoring.getScore(PAM250, A.charAt(i - 1), B.charAt(j - 1));
                int Delete = mat[j - 1][i] + (0 - 2);
                int Insert = mat[j][i - 1] + (0 - 2);
                mat[j][i] = Math.max(Math.max(Match,0), Math.max(Insert, Delete));
            }
        }

        // 再次遍历矩阵,获取矩阵最大值,用于查找序列初始点
        int tempMax = 0;
        int temprow =0,tempcol = 0;
        for (int i =0;i<row+1;i++) {
            for (int j =0;j<col+1;j++) {
                System.out.print(mat[i][j]+".");
                if (tempMax < mat[i][j]) {
                    tempMax = mat[i][j];
                    temprow = i;
                    tempcol = j;
                }
            }
            System.out.println();
        }

        String AlignmentA = "";
        String AlignmentB = "";
        int i = tempcol;
        int j = temprow;
        while (i > 1 && j > 1) {
            if (i > 1 && j > 1 && mat[j][i] == mat[j-1][i-1]
                    + Scoring.getScore(PAM250, A.charAt(i - 1), B.charAt(j - 1))) {
                AlignmentA = A.charAt(i - 1) + AlignmentA;
                AlignmentB = B.charAt(j - 1) + AlignmentB;
                i = i - 1;
                j = j - 1;
            } else if (i == 1 || j == 1) {
                break;
            } else if (j > 1 && mat[j][i] == mat[j - 1][i] + (0 - 2)) {
                AlignmentA = "-" + AlignmentA;
                AlignmentB = B.charAt(j - 1) + AlignmentB;
                j = j - 1;
            } else {
                AlignmentA = A.charAt(i - 1) + AlignmentA;
                AlignmentB = "-" + AlignmentB;
                i = i - 1;
            }
        }
        twoList.add(AlignmentA);
        twoList.add(AlignmentB);
        return twoList;
    }
}
* 其中给定罚分矩阵:
import java.util.HashMap;

//This stores scoring resources, including matrices

public class Scoring {

    //matrix contents are from rosalind:
     public static final int[][] BLOSUM62 = {
         { 4,  0, -2, -1, -2,  0, -2, -1, -1, -1, -1, -2, -1, -1, -1,  1,  0,  0, -3, -2},
         { 0,  9, -3, -4, -2, -3, -3, -1, -3, -1, -1, -3, -3, -3, -3, -1, -1, -1, -2, -2},
         {-2, -3,  6,  2, -3, -1, -1, -3, -1, -4, -3,  1, -1,  0, -2,  0, -1, -3, -4, -3},
         {-1, -4,  2,  5, -3, -2,  0, -3,  1, -3, -2,  0, -1,  2,  0,  0, -1, -2, -3, -2},
         {-2, -2, -3, -3,  6, -3, -1,  0, -3,  0,  0, -3, -4, -3, -3, -2, -2, -1,  1,  3},
         { 0, -3, -1, -2, -3,  6, -2, -4, -2, -4, -3,  0, -2, -2, -2,  0, -2, -3, -2, -3},
         {-2, -3, -1,  0, -1, -2,  8, -3, -1, -3, -2,  1, -2,  0,  0, -1, -2, -3, -2,  2},
         {-1, -1, -3, -3,  0, -4, -3,  4, -3,  2,  1, -3, -3, -3, -3, -2, -1,  3, -3, -1},
         {-1, -3, -1,  1, -3, -2, -1, -3,  5, -2, -1,  0, -1,  1,  2,  0, -1, -2, -3, -2},
         {-1, -1, -4, -3,  0, -4, -3,  2, -2,  4,  2, -3, -3, -2, -2, -2, -1,  1, -2, -1},
         {-1, -1, -3, -2,  0, -3, -2,  1, -1,  2,  5, -2, -2,  0, -1, -1, -1,  1, -1, -1},
         {-2, -3,  1,  0, -3,  0,  1, -3,  0, -3, -2,  6, -2,  0,  0,  1,  0, -3, -4, -2},
         {-1, -3, -1, -1, -4, -2, -2, -3, -1, -3, -2, -2,  7, -1, -2, -1, -1, -2, -4, -3},
         {-1, -3,  0,  2, -3, -2,  0, -3,  1, -2,  0,  0, -1,  5,  1,  0, -1, -2, -2, -1},
         {-1, -3, -2,  0, -3, -2,  0, -3,  2, -2, -1,  0, -2,  1,  5, -1, -1, -3, -3, -2},
         { 1, -1,  0,  0, -2,  0, -1, -2,  0, -2, -1,  1, -1,  0, -1,  4,  1, -2, -3, -2},
         { 0, -1, -1, -1, -2, -2, -2, -1, -1, -1, -1,  0, -1, -1, -1,  1,  5,  0, -2, -2},
         { 0, -1, -3, -2, -1, -3, -3,  3, -2,  1,  1, -3, -2, -2, -3, -2,  0,  4, -3, -1},
         {-3, -2, -4, -3,  1, -2, -2, -3, -3, -2, -1, -4, -4, -2, -3, -3, -2, -3, 11,  2},
         {-2, -2, -3, -2,  3, -3,  2, -1, -2, -1, -1, -2, -3, -1, -2, -2, -2, -1,  2,  7}};


    //This following matrix is copied from rosalind:
    public static final int[][] PAM250 = {
            {4,-1,-2,-2,0,-1,-1,0,-2,-1,-1,-1,-1,-2,-1,1,0,-3,-2,0},
            {-1,5,0,-2,-3,1,0,-2,0,-3,-2,2,-1,-3,-2,-1,-1,-3,-2,-3},
            {-2,0,6,1,-3,0,0,0,1,-3,-3,0,-2,-3,-2,1,0,-4,-2,-3},
            {-2,-2,1,6,-3,0,2,-1,-1,-3,-4,-1,-3,-3,-1,0,-1,-4,-3,-3},
            {0,-3,-3,-3,9,-3,-4,-3,-3,-1,-1,-3,-1,-2,-3,-1,-1,-2,-2,-1},
            {-1,1,0,0,-3,5,2,-2,0,-3,-2,1,0,-3,-1,0,-1,-2,-1,-2},
            {-1,0,0,2,-4,2,5,-2,0,-3,-3,1,-2,-3,-1,0,-1,-3,-2,-2},
            {0,-2,0,-1,-3,-2,-2,6,-2,-4,-4,-2,-3,-3,-2,0,-2,-2,-3,-3},
            {-2,0,1,-1,-3,0,0,-2,8,-3,-3,-1,-2,-1,-2,-1,-2,-2,2,-3},
            {-1,-3,-3,-3,-1,-3,-3,-4,-3,4,2,-3,1,0,-3,-2,-1,-3,-1,3},
            {-1,-2,-3,-4,-1,-2,-3,-4,-3,2,4,-2,2,0,-3,-2,-1,-2,-1,1},
            {-1,2,0,-1,-3,1,1,-2,-1,-3,-2,5,-1,-3,-1,0,-1,-3,-2,-2},
            {-1,-1,-2,-3,-1,0,-2,-3,-2,1,2,-1,5,0,-2,-1,-1,-1,-1,1},
            {-2,-3,-3,-3,-2,-3,-3,-3,-1,0,0,-3,0,6,-4,-2,-2,1,3,-1},
            {-1,-2,-2,-1,-3,-1,-1,-2,-2,-3,-3,-1,-2,-4,7,-1,-1,-4,-3,-2},
            {1,-1,1,0,-1,0,0,0,-1,-2,-2,0,-1,-2,-1,4,1,-3,-2,-2},
            {0,-1,0,-1,-1,-1,-1,-2,-2,-1,-1,-1,-1,-2,-1,1,5,-2,-2,0},
            {-3,-3,-4,-4,-2,-2,-3,-2,-2,-3,-2,-3,-1,1,-4,-3,-2,11,2,-3},
            {-2,-2,-2,-3,-2,-1,-2,-3,2,-1,-1,-2,-1,3,-3,-2,-2,2,7,-1},
            {0,-3,-3,-3,-1,-2,-2,-3,-3,3,1,-2,1,-1,-2,-2,0,-3,-1,4}};

    public static HashMap<Character, Integer> matrixIndex = new HashMap<>();

    //initializing values for the matrices, as they are arranged here:
    static {
        matrixIndex.put('A', 0);
        matrixIndex.put('C', 1);
        matrixIndex.put('D', 2);
        matrixIndex.put('E', 3);
        matrixIndex.put('F', 4);
        matrixIndex.put('G', 5);
        matrixIndex.put('H', 6);
        matrixIndex.put('I', 7);
        matrixIndex.put('K', 8);
        matrixIndex.put('L', 9);
        matrixIndex.put('M', 10);
        matrixIndex.put('N', 11);
        matrixIndex.put('P', 12);
        matrixIndex.put('Q', 13);
        matrixIndex.put('R', 14);
        matrixIndex.put('S', 15);
        matrixIndex.put('T', 16);
        matrixIndex.put('V', 17);
        matrixIndex.put('W', 18);
        matrixIndex.put('Y', 19);
    }

    // returns the score of a match between the two provided amino acids, as scored by the specified matrix:
    public static int getScore(int[][] matrix, char reference, char search) {
        reference = Character.toUpperCase(reference);
        search = Character.toUpperCase(search);

        return matrix[matrixIndex.get(reference)][matrixIndex.get(search)]; 
    }

}
* 对文本进行处理测试:
import java.io.*;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zoutai on 2017/10/2.
 */
public class TestAlignment03 {

    public static final int[][] PAM250 = {
            {4, -1, -2, -2, 0, -1, -1, 0, -2, -1, -1, -1, -1, -2, -1, 1, 0, -3, -2, 0},
            {-1, 5, 0, -2, -3, 1, 0, -2, 0, -3, -2, 2, -1, -3, -2, -1, -1, -3, -2, -3},
            {-2, 0, 6, 1, -3, 0, 0, 0, 1, -3, -3, 0, -2, -3, -2, 1, 0, -4, -2, -3},
            {-2, -2, 1, 6, -3, 0, 2, -1, -1, -3, -4, -1, -3, -3, -1, 0, -1, -4, -3, -3},
            {0, -3, -3, -3, 9, -3, -4, -3, -3, -1, -1, -3, -1, -2, -3, -1, -1, -2, -2, -1},
            {-1, 1, 0, 0, -3, 5, 2, -2, 0, -3, -2, 1, 0, -3, -1, 0, -1, -2, -1, -2},
            {-1, 0, 0, 2, -4, 2, 5, -2, 0, -3, -3, 1, -2, -3, -1, 0, -1, -3, -2, -2},
            {0, -2, 0, -1, -3, -2, -2, 6, -2, -4, -4, -2, -3, -3, -2, 0, -2, -2, -3, -3},
            {-2, 0, 1, -1, -3, 0, 0, -2, 8, -3, -3, -1, -2, -1, -2, -1, -2, -2, 2, -3},
            {-1, -3, -3, -3, -1, -3, -3, -4, -3, 4, 2, -3, 1, 0, -3, -2, -1, -3, -1, 3},
            {-1, -2, -3, -4, -1, -2, -3, -4, -3, 2, 4, -2, 2, 0, -3, -2, -1, -2, -1, 1},
            {-1, 2, 0, -1, -3, 1, 1, -2, -1, -3, -2, 5, -1, -3, -1, 0, -1, -3, -2, -2},
            {-1, -1, -2, -3, -1, 0, -2, -3, -2, 1, 2, -1, 5, 0, -2, -1, -1, -1, -1, 1},
            {-2, -3, -3, -3, -2, -3, -3, -3, -1, 0, 0, -3, 0, 6, -4, -2, -2, 1, 3, -1},
            {-1, -2, -2, -1, -3, -1, -1, -2, -2, -3, -3, -1, -2, -4, 7, -1, -1, -4, -3, -2},
            {1, -1, 1, 0, -1, 0, 0, 0, -1, -2, -2, 0, -1, -2, -1, 4, 1, -3, -2, -2},
            {0, -1, 0, -1, -1, -1, -1, -2, -2, -1, -1, -1, -1, -2, -1, 1, 5, -2, -2, 0},
            {-3, -3, -4, -4, -2, -2, -3, -2, -2, -3, -2, -3, -1, 1, -4, -3, -2, 11, 2, -3},
            {-2, -2, -2, -3, -2, -1, -2, -3, 2, -1, -1, -2, -1, 3, -3, -2, -2, 2, 7, -1},
            {0, -3, -3, -3, -1, -2, -2, -3, -3, 3, 1, -2, 1, -1, -2, -2, 0, -3, -1, 4}};

    public static void main(String[] args) throws FileNotFoundException,IOException{
        File txtFile = new File("data_hm.txt");
        BufferedReader br = new BufferedReader(new FileReader(txtFile));
        int lineNumber = 0;
        String line = null;
        StringBuffer t1 = new StringBuffer();
        StringBuffer t2 = new StringBuffer();
        List strArr1 = new LinkedList();
        List strArr2 = new LinkedList();
        int[] resultArr = new int[25];

        Boolean first = true;
        Boolean flag = true;
        int isget = 1;
        while ((line = br.readLine())!=null) {
            if(first) {
                first = false;
                continue;
            } else if(line.equals("")) {
                first = true;
                if(isget==2) {
                    strArr1.add(t1);
                    strArr2.add(t2);
                    t1 = new StringBuffer();
                    t2 = new StringBuffer();
                    isget--;
                    continue;
                }
                isget++;
                flag = !flag;
            } else {
                if(flag) {
                    t1.append(line);
                } else {
                    t2.append(line);
                }
            }
        }
        List<String> list = new LinkedList<String>();
        System.out.println(strArr1.size());
        for (int i =0;i<25;i++) {
            System.out.println(strArr1.get(i).toString());
            System.out.println(strArr2.get(i).toString());
            System.out.println();
            resultArr[i] = localAlig(strArr1.get(i).toString(), strArr2.get(i).toString());
        }
        System.out.println(resultArr);
        for (int score: resultArr) {
            System.out.println(score);
        }
    }

    private static int localAlig(String A, String B) {
        List<String> twoList = new LinkedList<String>();
        int allMax = 0;
        int col = A.length();
        int row = B.length();
        int[][] mat = new int[row + 1][col + 1];
        for (int i = 0; i < col + 1; i++) {
            mat[0][i] = -10;
        }
        for (int j = 0; j < row + 1; j++) {
            mat[j][0] = -10;
        }
        for (int i = 1; i < col+1; i++) {
            for (int j = 1; j < row+1; j++) {
                int Match = mat[j - 1][i - 1] + Scoring.getScore(PAM250, A.charAt(i - 1), B.charAt(j - 1));
                int Delete = mat[j - 1][i] + (0 - 2);
                int Insert = mat[j][i - 1] + (0 - 2);
                mat[j][i] = Math.max(Math.max(Match,0), Math.max(Insert, Delete));
            }
        }

        // 再次遍历矩阵,获取矩阵最大值,用于查找序列初始点
        int tempMax = 0;
        int temprow =0,tempcol = 0;
        for (int i =0;i<row+1;i++) {
            for (int j =0;j<col+1;j++) {
                System.out.print(mat[i][j]+".");
                if (tempMax < mat[i][j]) {
                    tempMax = mat[i][j];
                    temprow = i;
                    tempcol = j;
                }
            }
            System.out.println();
        }
        System.out.println(tempMax);
        String AlignmentA = "";
        String AlignmentB = "";
        int i = tempcol;
        int j = temprow;
        while (i > 1 && j > 1) {
            if (i > 1 && j > 1 && mat[j][i] == mat[j-1][i-1]
                    + Scoring.getScore(PAM250, A.charAt(i - 1), B.charAt(j - 1))) {
                AlignmentA = A.charAt(i - 1) + AlignmentA;
                AlignmentB = B.charAt(j - 1) + AlignmentB;
                i = i - 1;
                j = j - 1;
            } else if (i == 1 || j == 1) {
                break;
            } else if (j > 1 && mat[j][i] == mat[j - 1][i] + (0 - 2)) {
                AlignmentA = "-" + AlignmentA;
                AlignmentB = B.charAt(j - 1) + AlignmentB;
                j = j - 1;
            } else {
                AlignmentA = A.charAt(i - 1) + AlignmentA;
                AlignmentB = "-" + AlignmentB;
                i = i - 1;
            }
        }
        twoList.add(AlignmentA);
        twoList.add(AlignmentB);
        //return twoList;
        return tempMax;
    }
}

第一题:查找最长子序列
第二题:序列比较算法,使用最普通的方式,Ws = 2, Wd = 0, 无罚分矩阵,默认(相等:不相等)=(+3,-1)
第三题:相对于第二题,添加了罚分矩阵PAM250,(20*20);同时simple local alignment algorithm使用线性罚分,Wd=0,Ws=2;
affine-gap local alignment algorithm使用affine罚分,Wd=10,Ws=2。

一题:求两个序列的最长公共子序列或子串(动态规划)
参考链接:http://www.geeksforgeeks.org/longest-common-subsequence/

第二题:
参考:https://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm
https://en.wikipedia.org/wiki/Smith%E2%80%93Waterman_algorithm#Example

第三题:
参考github用户:https://github.com/hswaffield/Linear-Space-Local-Alignment
http://www.cs.utexas.edu/~mobios/cs329e/rosetta/src/Blosum.java
https://en.wikipedia.org/wiki/Gap_penalty#Affine
https://en.wikipedia.org/wiki/Smith%E2%80%93Waterman_algorithm#Gap_penalty

相关资料连接链接:http://pan.baidu.com/s/1nvaENdn 密码:gmyf

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

序列比对算法-计算生物学 的相关文章

随机推荐

  • 程序开发过程中的传值问题

    一 传参方式 单个值 二 传参方式 url传递多个值 用 三 传参方式 1 url传数组 2 url传多个参数 需要用 分号分割 3 案例 A页面向B页面传值几个步骤 1 先在A页面写单选提交事件传值 2 在传入页面B页面的onload里面
  • 高速下载百度网盘资料(Tampermonkey+百度网盘直链下载助手+xdown)

    下载百度网盘中的游戏 电影等文件时 由于百度自身对下载速度的限制 非VIP用户总是无法全速下载 下载速度一般在100KB s左右 如果短时间内下载文件 gt 10G还会有更严苛的下载速度限制 一般在50KB s 一周后解除限速 一旦我们想下
  • Unity5.x Animator之RootMotion

    Unity3D 中 Generic 动画导入设置和 Root Motion 之间的关系 Unity3D 的 Mecanim 动画系统可以直接复用 3DS MAX 中制作的动画文件中的位移 这个就是通过 applyRootMotion 来达成
  • UnityNative Plugin 导出时遇到的坑

    必须要在链接的输入里面添加模块定义文件 文件大概是这样 就是为了阻止名称混淆 否则UnityPluginLoad UnityPluginUnload这两个函数无法自动被Unity加载 file used by Visual Studio p
  • 死磕 java同步系列之JMM(Java Memory Model)

    简介 Java内存模型是在硬件内存模型上的更高层的抽象 它屏蔽了各种硬件和操作系统访问的差异性 保证了Java程序在各种平台下对内存的访问都能达到一致的效果 硬件内存模型 在正式讲解Java的内存模型之前 我们有必要先了解一下硬件层面的一些
  • Linux中三种引号(单引号、双引号、反引号)的区别

    1 双引号 保护特殊元字符和通配符不被shell解析 但是允许变量和命令的解析 以及转义符的解析 2 单引号 单引号内不允许任何变量 元字符 通配符 转义符被shell解析 均被原样输出 使用双引号或反斜杠转义可显示输出单引号 但是双引号和
  • 创建型设计模式之抽象工厂(Abstract Factory)模式

    定义 为创建一组相关或相互依赖的对象提供一个接口 而且无需指定他们的具体类 用意 客户端在不必指定产品的具体类型情况下 创建多个产品族中某个产品对象 定义图 参与者 抽象工厂 Creator 工厂方法核心 由一个接口或抽象类实现 具体工厂类
  • 【每日知识】点击下载按钮

    代码如下 methods OncilckUpload let link document createElement a 创建一个a标签 link style display none 将a标签隐藏 link href https api
  • $set在vue中的应用案例

    vue中this set在官方API中是这样说的 Vue set target propertyName index value 参数 Object Array target string number propertyName index
  • java api如何获取kafka所有Topic列表,并放置为一个list

    kafka内部所有的实现都是通过TopicCommand的main方法 通过java代码调用API TopicCommand main options 的方式只能打印到控制台 不能转换到一个list 下面讲解下如何转换为list 1 查看主
  • 三分钟快速搭建家纺行业小程序商城

    在互联网时代 越来越多的企业开始意识到小程序的重要性和价值 家纺行业也不例外 许多家纺企业开始关注和投入小程序商城的建设 然而 对于大多数家纺企业来说 搭建一个小程序商城可能会显得十分困难和复杂 但是 通过乔拓云平台 你只需要三分钟 就可以
  • 【Stm32野火】:野火STM32F103指南者开发板烧写官方示例程序LCD无法点亮?LCD示例程序无法使用?

    项目场景 大家好 最近在使用野火STM32F103指南者开发板的时候发现官方的示例程序LCD驱动代码居然无法直接驱动LCD点亮 这让我百思不得其解 以下就是我的踩坑填坑的过程 希望对大家有所帮助 野火官方资料下载文档链接 野火STM32F1
  • 计算机核心期刊排名及投稿信息

    1 计算机学报 北京 中国计算机学会等 2 软件学报 北京 中国科学院软件研究所 3 计算机研究与发展 北京 中国科学院计算技术研究所等 4 自动化学报 北京 中国科学院等 5 计算机科学 重庆 国家科技部西南信息中心 6 控制理论与应用
  • [Python图像处理] 三十七.OpenCV直方图统计两万字详解(掩膜直方图、灰度直方图对比、黑夜白天预测)

    该系列文章是讲解Python OpenCV图像处理知识 前期主要讲解图像入门 OpenCV基础用法 中期讲解图像处理的各种算法 包括图像锐化算子 图像增强技术 图像分割等 后期结合深度学习研究图像识别 图像分类应用 希望文章对您有所帮助 如
  • 线程池源码分析(一)

    最近在阅读 阿里巴巴Java开发手册 的时候 书中有这么一段话 线程池这块理解不是很深 今天就抽时间重新学习一遍 对于书中的问题分析完成后答案便一目了然 创建线程池的一个方式 ExecutorService e Executors newF
  • 三维重建_COLMAP安装、使用和参数说明(翻译自官方文档)

    近期因为想要入选学校某位很厉害的老师的某个项目 布置的小任务就是先把colmap以及openMVS跑一跑 我就记录了一下学习的经过 一 Ubuntu上源码编译colmap 参考网址 https colmap github io instal
  • 单片机串口控制树莓派3B播放HDMI视频,omxplayer,

    使用树莓派3B通过HDMI播放视频 并且使用串口去控制播放哪个视频 1 问题解耦 单片机串口控制树莓派3B播放视频 树莓派播放视频 单片机串口传参控制树莓派 树莓派播放视频 树莓派播放视频 并且能用串口这种简单的通信方式去控制 应该是需要安
  • java实现写大量数据到文件中

    生成 txt文件 生成 csv文件 生成 xls文件 import java io BufferedWriter import java io File import java io FileOutputStream import java
  • 【缓存】缓存,这么用才真正达到缓存的效果

    1 概述 转载 https zhuanlan zhihu com p 62508629 一 什么是缓存 平常的开发项目中 多多少少都会使用到缓存 因为一些数据我们没有必要每次查询的时候都去查询到数据库 一个形象的比喻 数据库是人的身体 缓存
  • 序列比对算法-计算生物学

    1 序列比对指将两个或多个序列排列在一起 标明其相似之处 序列中可以插入间隔 通常用短横线 表示 对应的相同或相似的符号 在核酸中是A T 或U C G 在蛋白质中是氨基酸残基的单字母表示 排列在同一列上 这一方法常用于研究由共同祖先进化而