JAL-2403 JAL-1483 changes to ScoreModelI hierarchy and signatures to
[jalview.git] / src / jalview / analysis / scoremodels / SWDistanceModel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.analysis.scoremodels;
22
23 import jalview.analysis.AlignSeq;
24 import jalview.api.analysis.DistanceScoreModelI;
25 import jalview.datamodel.AlignmentView;
26 import jalview.datamodel.SequenceI;
27 import jalview.math.Matrix;
28 import jalview.math.MatrixI;
29 import jalview.util.Comparison;
30
31 public class SWDistanceModel implements DistanceScoreModelI
32 {
33
34   @Override
35   public MatrixI findDistances(AlignmentView seqData)
36   {
37     SequenceI[] sequenceString = seqData.getVisibleAlignment(
38             Comparison.GAP_SPACE).getSequencesArray();
39     int noseqs = sequenceString.length;
40     double[][] distances = new double[noseqs][noseqs];
41
42     double max = -1;
43
44     for (int i = 0; i < (noseqs - 1); i++)
45     {
46       for (int j = i; j < noseqs; j++)
47       {
48         AlignSeq as = new AlignSeq(sequenceString[i], sequenceString[j],
49                 seqData.isNa() ? "dna" : "pep");
50         as.calcScoreMatrix();
51         as.traceAlignment();
52         as.printAlignment(System.out);
53         distances[i][j] = as.maxscore;
54
55         if (max < distances[i][j])
56         {
57           max = distances[i][j];
58         }
59       }
60     }
61
62     for (int i = 0; i < (noseqs - 1); i++)
63     {
64       for (int j = i; j < noseqs; j++)
65       {
66         distances[i][j] = max - distances[i][j];
67         distances[j][i] = distances[i][j];
68       }
69     }
70
71     return new Matrix(distances);
72   }
73
74   @Override
75   public String getName()
76   {
77     return "Smith Waterman Score";
78   }
79
80   @Override
81   public boolean isDNA()
82   {
83     return true;
84   }
85
86   @Override
87   public boolean isProtein()
88   {
89     return true;
90   }
91
92   @Override
93   public String toString()
94   {
95     return "Score between two sequences aligned with Smith Waterman with default Peptide/Nucleotide matrix";
96   }
97 }