软件编程
位置:首页>> 软件编程>> java编程>> java实现基因序列比较的示例代码

java实现基因序列比较的示例代码

作者:※回不去×  发布时间:2022-01-03 02:32:22 

标签:java,基因序列

设计算法,计算两给定基因序列的相似程度。

人类基因由4种核苷酸,分别用字母ACTG表示。要求编写一个程序,按以下规则比较两个基因序列并确定它们的相似程度。即给出两个基因序列AGTGATG和GTTAG,它们有多相似呢?测量两个基因相似度的一种方法称为对齐。使用对齐方法可以在基因的适当位置加入空格,让两个基因的长度相等,然后根据基因的分值矩阵计算分数。

看了很多代码基本上都是用c++或者c写的,但是习惯性写java就用java实现一下

java实现基因序列比较的示例代码

基本的思路就是,和背包问题差不多,实现还是模仿填表的形式去实现的

表达式:

  • s1 = result[i-1][j-1] + getScore(X[i], Y[j]) 这个是x,y序列使用坐标匹配

  • s2 = result[i-1][j] + getScore(X[i], ‘-') 这个是x序列匹配y的 ‘-'

  • s3 = result[i][j-1] + getScore('-', Y[j]) 这个是y序列匹配x的 ‘-'

  • result[i][j] = max(s1,s2,s3) 找出三个中最大的就是所求的值


package algorithmClassSet.three;

import java.util.HashMap;
import java.util.Map;

/**
* s1 = result[i-1][j-1] + getScore(X[i], Y[j]) 这个是x,y序列使用坐标匹配
* s2 = result[i-1][j] + getScore(X[i], '-')  这个是x序列匹配y的 ‘-'
* s3 = result[i][j-1] + getScore('-', Y[j]) 这个是y序列匹配x的 ‘-'
* result[i][j] = max(s1,s2,s3)  找出三个中最大的就是所求的值
* m*n
*/

public class GeneSequenceComparison {
 public static void main(String[] args) {
   dealIt();
 }

private static void dealIt() {
   String[] X = {"A", "G", "T", "G", "A", "T", "G"};
   String[] Y = {"G", "T", "T", "A", "G"};
   int m = X.length + 1;
   int n = Y.length + 1;
   int[][] result = new int[m][n];

for (int i = 1; i < m; i++) {
     result[i][0] = result[i - 1][0] + getScore(X[i - 1], "-");
   }
   for (int j = 1; j < n; j++) {
     result[0][j] = result[0][j - 1] + getScore("-", Y[j - 1]);
   }

for (int i = 1; i < m; i++) {
     for (int j = 1; j < n; j++) {
       int s1 = result[i - 1][j - 1] + getScore(X[i - 1], Y[j - 1]);
       int s2 = result[i - 1][j] + getScore(X[i - 1], "-");
       int s3 = result[i][j - 1] + getScore("-", Y[j - 1]);
       int maxs = getMax(s1, s2, s3);
       result[i][j] = maxs;
     }
   }
   System.out.println("结果为:" + result[m - 1][n - 1]);

for (int i = 0; i < m; i++) {
     for (int j = 0; j < n; j++) {
       System.out.print(result[i][j] + " ");
     }
     System.out.println();
   }
 }

private static int getMax(int s1, int s2, int s3) {
   int flag = s1;
   if (flag < s2) {
     flag = s2;
   }
   if (flag < s3) {
     flag = s3;
   }
   return flag;
 }

//传入值获取分数
 private static int getScore(String x, String y) {
   //x和y必须属于 ACGT-
   Map<String, Integer> map = new HashMap<>();
   map.put("A", 0);
   map.put("C", 1);
   map.put("G", 2);
   map.put("T", 3);
   map.put("-", 4);
   int[][] score = {
       {5, -1, -2, -1, -3},
       {-1, 5, -3, -2, -4},
       {-2, -3, 5, -2, -2},
       {-1, -2, -2, 5, -1},
       {-3, -4, -2, -1, -10000000}};
   return score[map.get(x)][map.get(y)];
 }
}

java实现基因序列比较的示例代码

来源:https://blog.csdn.net/qq_45049981/article/details/104414059

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com