*Joyful Bioinfo가 직접 작성하였습니다.
import java.io.*; // Import the File class
import java.util.*; // Import the Scanner class to read text files
public class NeedlemanWunsch {
public static List<String> ReadFile(String input) throws IOException{
FileReader myReader = new FileReader(input);
BufferedReader bufReader = new BufferedReader(myReader);
List<String> Data = new ArrayList<String>();
String line = "";
String ATGC = "";
String header = "";
while ((line = bufReader.readLine()) != null) {
if (line.contains(">") == true) {
header += line;
}
else ATGC += line.toUpperCase();
}
Data.add(header);
Data.add(ATGC);
bufReader.close();
return Data;
}
public static String[] ReadSequence(List<String> Seq1,List<String> Seq2,int Matchs,int Miss, int Gaps) {
double nMatch = Matchs;
double nMis = Miss;
double nGap = Gaps;
String seq1 = Seq1.get(1);
String seq2 = Seq2.get(1);
int seq1_length = seq1.length();
int seq2_length = seq2.length();
int Longer_seq_length = 0;
if (seq1_length > seq2_length) Longer_seq_length = seq1_length;
else Longer_seq_length = seq2_length;
double[][] ScoreMatrix = new double[seq1_length+1][seq2_length+1];
double[][] SavePath = new double[seq1_length+1][seq2_length+1]; //1:Diagonal,Left,Upper
ScoreMatrix[0][0] = 0;
// Set up the first Scores! :)
for (int i = 1; i < (seq1_length+1) ; i++) {
ScoreMatrix[i][0] = ScoreMatrix[i-1][0]+nGap;
SavePath[i][0]=0;
}
for (int j = 1; j < (seq2_length+1) ; j++) {
ScoreMatrix[0][j] = ScoreMatrix[0][j-1]+nGap;
SavePath[0][j]=0;
}
// Fill out the rest of Matrix
for ( int Col = 1; Col < (seq1_length+1) ; Col ++) {
for (int Row = 1; Row <(seq2_length+1); Row ++) {
double Diagonal = ScoreMatrix[Col-1][Row-1];
double Left = ScoreMatrix[Col-1][Row];
double Upper = ScoreMatrix[Col][Row-1];
Character cSeq1 = seq1.charAt(Col-1);
Character cSeq2 = seq2.charAt(Row-1);
// Diagonal
if (cSeq1.equals(cSeq2)==true)
Diagonal += nMatch;
else Diagonal += nMis;
// Left
Left += nGap;
//Right
Upper += nGap;
//Select Max
double[] result = FindMax(Diagonal,Left,Upper);
double Max = result[0];
double Path = result[1];
ScoreMatrix[Col][Row]=Max;
SavePath[Col][Row]=Path;
}
}
//System.out.println(Arrays.deepToString(ScoreMatrix));
//System.out.println(Arrays.deepToString(SavePath));
/// Trace back!
String[] Result = new String[3];
String Alig_seq1 = "";
String Alig_seq2 = "";
int TrackRow = seq1_length;
int TrackCol = seq2_length;
//System.out.println(seq1);
double AlignScore = SavePath[TrackRow][TrackCol];
for (int t = 0; t < (Longer_seq_length+2); t ++) {
double Path_start = SavePath[TrackRow][TrackCol];
if (Path_start == 1) {
Alig_seq1 += seq1.charAt(TrackRow-1);
Alig_seq2 += seq2.charAt(TrackCol-1);
//System.out.println(seq1.charAt(TrackRow-1));
//System.out.println(TrackRow-1);
//System.out.println(Alig_seq1);
TrackRow -=1;
TrackCol -=1;
}
else if (Path_start == 2) {
Alig_seq2 += "-";
Alig_seq1 += seq1.charAt(TrackRow-1);
TrackRow -=1;
}
else if (Path_start == 3) {
Alig_seq1 += "-";
Alig_seq2 += seq2.charAt(TrackCol-1);
TrackCol -=1;
}
}
//System.out.println(Alig_seq1);
//System.out.println(Alig_seq2);
Result[0] = Alig_seq1;
Result[1] = Alig_seq2;
Result[2] = String.valueOf(ScoreMatrix[seq1_length][seq2_length]);
return Result;
}
public static double[] FindMax(double num1, double num2, double num3) {
double Max = 0;
double Path = 0;
if (num1 >= num2 && num1 >= num3) {
Max = num1;
Path = 1;}
else if (num2 >= num1 && num2 >= num3)
{Max = num2;
Path = 2;}
else {
Max = num3;
Path = 3;}
double[]Result= new double[2];
Result[0]=Max;
Result[1]=Path;
return Result;
}
public static void WriteFile(List<String> Data1,List<String> Data2, String[] Result, String output) throws IOException{
FileWriter outfile = new FileWriter(output);
outfile.write("Sequence1 :"+Data1.get(0)+"\n");
outfile.write(Data1.get(1)+"\n");
outfile.write("Length: ");
outfile.write(Data1.get(1).length()+"\n\n");
outfile.write("Sequence2 :"+Data2.get(0)+"\n");
outfile.write(Data2.get(1)+"\n");
outfile.write("Length: ");
outfile.write(Data2.get(1).length()+"\n\n");
outfile.write("Alignment Scores: " +Result[2]+"\n");
outfile.write("Alignment: \n");
String A1 = reverse(Result[0]);
String A2 = reverse(Result[1]);
//System.out.println(Result[0]);
//System.out.println(A1);
int Count_a1 = 0;
int Count_a2 = 0;
for (int Group = 0; Group < A1.length()-1;Group+=60) {
int End =0 ;
if(A1.length()-Group>60) End = 60;
else End = A1.length()-Group;
// Seq1!
for (int t = 0; t < End ; t ++) {
//System.out.println(t);
char a1 = A1.charAt(t+Group);
//System.out.println(a1);
if (t == 0) {
outfile.write("\n");
outfile.write(Count_a1+" : ");
}
outfile.write(a1);
if (a1 != '-')Count_a1+=1; }
// ****
for (int t3 = 0; t3 < End ; t3 ++) {
char W = ' ';
if (t3 == 0) {
outfile.write("\n ");
if (Group>10) outfile.write(" ");
if(Group>100) outfile.write(" ");
if(Group>1000) outfile.write(" ");
}
if (A1.charAt(t3+Group)==A2.charAt(t3+Group)) W = '*';
outfile.write(W);
}
//Seq2!
for (int t2 = 0; t2 < End ; t2 ++) {
char a2 = A2.charAt(t2+Group);
if (t2 == 0) {
outfile.write("\n");
outfile.write(Count_a2+" : ");
}
outfile.write(a2);
if (a2 != '-')Count_a2+=1;
}
}
outfile.close();
}
public static String reverse(String input){
char[] tmp = input.toCharArray();
int begin=0;
int end=tmp.length-1;
char temp;
while(end>begin){
temp = tmp[begin];
tmp[begin]=tmp[end];
tmp[end] = temp;
end--;
begin++;
}
return new String(tmp);
}
//public static void main(String args[]) throws IOException{
public static void main(String args[]) throws IOException{
if(args.length <5) System.out.println("Usage : java NeedlemanWunsch [seq file1] [seq file2] [Match score] [Mismatch score] [Gap panalty] [outfileName]");
else{
String input1 = args[0];
String input2 = args[1];
List<String> Seq1 = ReadFile(input1);
List<String> Seq2 = ReadFile(input2);
String outfile = args[5];
int Matchs = Integer.parseInt(args[2]);
int Miss = Integer.parseInt(args[3]);
int Gaps = Integer.parseInt(args[4]);
String[] Result = ReadSequence(Seq1, Seq2,Matchs,Miss,Gaps);
WriteFile(Seq1, Seq2, Result,outfile) ;
}
}
}