更多代码请见:https://github.com/xubo245
基因数据处理系列
1.解释
先有java提供转换,使用jni调用c
然后scala调用java
2.代码:
2.1 java:
package ssw;
/**
* Created by xubo on 2016/11/25.
*/
public class SSW {
public static int align(String query, String ref) {
try {
System.loadLibrary("sswjni");
} catch (UnsatisfiedLinkError e) {
System.out.println(String.format("Cannot find libsswjni.so. Has the library been built and LD_LIBRARY_PATH or -Djava.library.path set appropriately?\n%s", e));
throw e;
}
int[][] score = new int[128][128];
for (int i = 0; i < 128; i++) {
for (int j = 0; j < 128; j++) {
if (i == j) score[i][j] = 2;
else score[i][j] = -2;
}
}
System.out.println("Aligning nucleotides");
Alignment aln = Aligner.align(query.getBytes(), ref.getBytes(), score, 12, 2, true);
if (aln == null) {
throw new RuntimeException();
}
return aln.score1;
}
public static int[][] blosum50() {
int[][] a = {
{5, -2, -1, -2, -1, -3, 0, -2, -1, -2, -1, -2, -1, -1, -5, -1, -1, -2, 1, 0, -5, 0, -3, -1, -2, -1},
{-2, 6, -3, 6, 1, -4, -1, 0, -4, -4, 0, -4, -3, 5, -5, -2, 0, -1, 0, 0, -5, -3, -5, -1, -3, 1},
{-1, -3, 13, -4, -3, -2, -3, -3, -2, -2, -3, -2, -2, -2, -5, -4, -3, -4, -1, -1, -5, -1, -5, -1, -3, -3},
{-2, 6, -4, 8, 2, -5, -1, -1, -4, -4, -1, -4, -4, 2, -5, -1, 0, -2, 0, -1, -5, -4, -5, -1, -3, 1},
{-1, 1, -3, 2, 6, -3, -3, 0, -4, -3, 1, -3, -2, 0, -5, -1, 2, 0, -1, -1, -5, -3, -3, -1, -2, 5},
{-3, -4, -2, -5, -3, 8, -4, -1, 0, 1, -4, 1, 0, -4, -5, -4, -4, -3, -3, -2, -5, -1, 1, -1, 4, -4},
{0, -1, -3, -1, -3, -4, 8, -2, -4, -4, -2, -4, -3, 0, -5, -2, -2, -3, 0, -2, -5, -4, -3, -1, -3, -2},
{-2, 0, -3, -1, 0, -1, -2, 10, -4, -3, 0, -3, -1, 1, -5, -2, 1, 0, -1, -2, -5, -4, -3, -1, 2, 0},
{-1, -4, -2, -4, -4, 0, -4, -4, 5, 4, -3, 2, 2, -3, -5, -3, -3, -4, -3, -1, -5, 4, -3, -1, -1, -3},
{-2, -4, -2, -4, -3, 1, -4, -3, 4, 4, -3, 4, 2, -4, -5, -3, -3, -3, -3, -1, -5, 2, -2, -1, -1, -3},
{-1, 0, -3, -1, 1, -4, -2, 0, -3, -3, 6, -3, -2, 0, -5, -1, 2, 3, 0, -1, -5, -3, -3, -1, -2, 1},
{-2, -4, -2, -4, -3, 1, -4, -3, 2, 4, -3, 5, 3, -4, -5, -4, -2, -3, -3, -1, -5, 1, -2, -1, -1, -3},
{1, -3, -2, -4, -2, 0, -3, -1, 2, 2, -2, 3, 7, -2, -5, -3, 0, -2, -2, -1, -5, 1, -1, -1, 0, -1},
{-1, 5, -2, 2, 0, -4, 0, 1, -3, -4, 0, -4, -2, 7, -5, -2, 0, -1, 1, 0, -5, -3, -4, -1, -2, 0},
{-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, 1, -5, -5, -5, -5, -5, -5, -5, -5, -5, 0, -5},
{-1, -2, -4, -1, -1, -4, -2, -2, -3, -3, -1, -4, -3, -2, -5, 10, -1, -3, -1, -1, -5, -3, -4, -1, -3, -1},
{-1, 0, -3, 0, 2, -4, -2, 1, -3, -3, 2, -2, 0, 0, -5, -1, 7, 1, 0, -1, -5, -3, -1, -1, -1, 4},
{-2, -1, -4, -2, 0, -3, -3, 0, -4, -3, 3, -3, -2, -1, -5, -3, 1, 7, -1, -1, -5, -3, -3, -1, -1, 0},
{1, 0, -1, 0, -1, -3, 0, -1, -3, -3, 0, -3, -2, 1, -5, -1, 0, -1, 5, 2, -5, -2, -4, -1, -2, 0},
{0, 0, -1, -1, -1, -2, -2, -2, -1, -1, -1, -1, -1, 0, -5, -1, -1, -1, 2, 5, 1, 0, -3, -1, -2, -1},
{-2, -1, -2, -3, -3, -1, -2, -3, 2, -1, -1, -2, 0, 4, 0, -3, -2, -2, 2, 8, 0, -1, -3, -1, -2, -1},
{0, -3, -1, -4, -3, -1, -4, -4, 4, 2, -3, 1, 1, -3, -5, -3, -3, -3, -2, 0, -5, 5, -3, -1, -1, -3},
{-3, -5, -5, -5, -3, 1, -3, -3, -3, -2, -3, -2, -1, -4, -5, -4, -1, -3, -4, -3, -5, -3, 15, -1, 2, -2},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -5, -1, -1, -1, -1, -1, -5, -1, -1, -1, -1, -1},
{-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, 8, -5},
{-1, 1, -3, 1, 5, -4, -2, 0, -3, -3, 1, -3, -1, 0, -5, -1, 4, 0, 0, -1, -5, -3, -2, -1, -2, 5}
};
return a;
}
}
2.2 scala调用:
package org.dsw.other.ssw
import ssw.SSW
/**
* Created by xubo on 2016/11/25.
*/
object SSWLearning {
def main(args: Array[String]) {
val score = SSW.align("AGCT", "ACT")
println(score)
}
}
3.结果:
xubo@xubo:~/xubo/tools/Complete-Striped-Smith-Waterman-Library/src$ scala -Djava.library.path=. -cp SparkSW.jar org.dsw.other.ssw.SSWLearning
Aligning nucleotides
4
参考
【1】https://github.com/xubo245
【2】http://blog.csdn.net/xubo245/