2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.analysis.scoremodels;
23 import jalview.api.AlignmentViewPanel;
24 import jalview.api.FeatureRenderer;
25 import jalview.api.analysis.DistanceScoreModelI;
26 import jalview.api.analysis.ViewBasedAnalysisI;
27 import jalview.datamodel.AlignmentView;
28 import jalview.datamodel.SeqCigar;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.math.Matrix;
31 import jalview.math.MatrixI;
32 import jalview.util.SetUtils;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.List;
40 public class FeatureDistanceModel implements DistanceScoreModelI,
46 public boolean configureFromAlignmentView(AlignmentViewPanel view)
49 fr = view.cloneFeatureRenderer();
54 * Calculates a distance measure [i][j] between each pair of sequences as the
55 * average number of features they have but do not share. That is, find the
56 * features each sequence pair has at each column, ignore feature types they
57 * have in common, and count the rest. The totals are normalised by the number
58 * of columns processed.
61 public MatrixI findDistances(AlignmentView seqData)
63 List<String> dft = fr.getDisplayedFeatureTypes();
64 SeqCigar[] seqs = seqData.getSequences();
65 int noseqs = seqs.length;
66 int cpwidth = 0;// = seqData.getWidth();
67 double[][] distances = new double[noseqs][noseqs];
70 return new Matrix(distances);
73 // need to get real position for view position
74 int[] viscont = seqData.getVisibleContigs();
77 * scan each column, compute and add to each distance[i, j]
78 * the number of feature types that seqi and seqj do not share
80 for (int vc = 0; vc < viscont.length; vc += 2)
82 for (int cpos = viscont[vc]; cpos <= viscont[vc + 1]; cpos++)
87 * first pass: record features types in column for each sequence
89 Map<SeqCigar, Set<String>> sfap = findFeatureTypesAtColumn(
93 * count feature types on either i'th or j'th sequence but not both
94 * and add this 'distance' measure to the total for [i, j] for j > i
96 for (int i = 0; i < (noseqs - 1); i++)
98 for (int j = i + 1; j < noseqs; j++)
100 int seqDistance = SetUtils.countDisjunction(sfap.get(seqs[i]),
102 distances[i][j] += seqDistance;
109 * normalise the distance scores (summed over columns) by the
110 * number of visible columns used in the calculation
111 * and fill in the bottom half of the matrix
113 // TODO JAL-2424 cpwidth may be out by 1 - affects scores but not tree shape
114 for (int i = 0; i < noseqs; i++)
116 for (int j = i + 1; j < noseqs; j++)
118 distances[i][j] /= cpwidth;
119 distances[j][i] = distances[i][j];
122 return new Matrix(distances);
126 * Builds and returns a list (one per SeqCigar) of visible feature types at
127 * the given column position
130 * @param columnPosition
133 protected Map<SeqCigar, Set<String>> findFeatureTypesAtColumn(
134 SeqCigar[] seqs, int columnPosition)
136 Map<SeqCigar, Set<String>> sfap = new HashMap<SeqCigar, Set<String>>();
137 for (SeqCigar seq : seqs)
139 Set<String> types = new HashSet<String>();
140 int spos = seq.findPosition(columnPosition);
143 List<SequenceFeature> sfs = fr.findFeaturesAtRes(seq.getRefSeq(),
145 for (SequenceFeature sf : sfs)
147 types.add(sf.getType());
150 sfap.put(seq, types);
156 public String getName()
158 return "Sequence Feature Similarity";
162 public boolean isDNA()
168 public boolean isProtein()
174 public String toString()
176 return "Score between sequences based on hamming distance between binary vectors marking features displayed at each column";